<?php
define('IN_WOGU', true);

class App
{
    protected $_init = false;
    protected $_initDb = false;
    protected $_initSession = false;
    static protected $_instance = null;

    protected function __construct() {}
    protected function __clone() {}

    static public function getInstance() {
        if(null === self::$_instance) {
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }

    public function init() {
        if($this->_init) {
            return true;
        }
        
        define('SYS_TIME', time());
        define('WOGU_ROOT', substr(dirname(__FILE__), 0, -4));
        define('LIB_ROOT', WOGU_ROOT . 'wogu' . DIRECTORY_SEPARATOR);
        define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');
        define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
        define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
        include LIB_ROOT. 'libs/classes/Loader.class.php';
        include LIB_ROOT. 'libs/classes/Register.class.php';
        Loader::loadConfig('common');
        Loader::loadFunc('global');
        define('IP', ip());
        
        $this->_init = true;
        return $this;
    }

    public function initDb() {
        if($this->_initDb) {
            return true;
        }
        
        Loader::loadClass('Db_Factory');
        Db_Factory::init();
        
        $this->_initDb = true;
        return $this;
    }
    
    public function initSession($sessType = 'Session_Mysql') {
        if($this->_initSession) {
            return true;
        }
        
        Loader::loadClass('Session');
        Session::init($sessType);
        
        $this->_initSession = true;
        return $this;
    }

    public function dispatch(Router $router = null, Dispatcher $dispatcher = null, Request $request = null, Respones $respones = null) {
        Loader::loadClass('PluginManager');
        Loader::loadClass('Request');
        Loader::loadClass('Respones');
        Loader::loadClass('Dispatcher');
        Loader::loadClass('Router');
        
        if(!$request) {
            $request = new Request();
        }
        
        if(!$respones) {
            $respones = new Respones();
        }
        
        if(!$dispatcher) {
            $dispatcher = new Dispatcher();
        }
        
        if(!$router) {
            $router = new Router();
        }
        
        //注册变量
        Register::set('Request', $request, 'WOGU_SYS');
            
        //路由
        $router->route($request);
        
        //分派循环开始
        PluginManager::dispatchLoopStartup($request, $respones);

        do {
            $request->setDispatched(true);
            
            PluginManager::preDispatch($request, $respones);
            if (!$request->isDispatched()) {
                continue;
            }
            
            $dispatcher->dispatch($request, $respones);
            
            PluginManager::postDispatch($request, $respones);
        } while(!$request->isDispatched());
            
        //分派循环结束
        PluginManager::dispatchLoopShutdown($request, $respones);
        
        return $respones->sendRespones();
    }
}